import java.util.Stack;

/**
 * 1.入栈的条件？压入一半，之后弹出与剩下的开始比较
 * 2.判定（）这个是一对 错了
 * <p>
 * 1.如果是 { [ (就压入栈中
 * 2.否则就弹出：然后比较栈顶
 */

public class Solution20 {
    public boolean isValid(String s) {
        int n = s.length();
        if (n % 2 == 1) {
            return false;
        }

        Stack<Character> stack = new Stack<>();

        for (int i = 0; i < n; i++) {
            char c = s.charAt(i);
            if (c == '[' || c == '(' || c == '{') {
                stack.push(c);
            } else if (stack.isEmpty() || c == ']' && stack.pop() != '[' || c == '}' && stack.pop() != '{' || c == ')' && stack.pop() != '(') {
                return false;
            }
        }
        return stack.isEmpty();
    }

    public static void main(String[] args) {
        String s = "{{";

        boolean valid = new Solution20().isValid(s);
        System.out.println(valid);
    }
}
